package com.jungle.client.demo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class TableViewWithComboBox extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Create the TableView
        TableView<Person> tableView = new TableView<>();

        // Create columns
        TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));

        TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
        genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));

        // Define the ComboBox options
        ObservableList<String> genderOptions = FXCollections.observableArrayList("Male", "Female", "Other");

        // Set up the ComboBox for the gender column
        genderColumn.setCellFactory(ComboBoxTableCell.forTableColumn(new StringConverter<>() {
            @Override
            public String toString(String object) {
                return object;
            }

            @Override
            public String fromString(String string) {
                return string;
            }
        }, genderOptions));

        // Allow editing of the gender column
        tableView.setEditable(true);
        genderColumn.setEditable(true);

        // Add columns to the table
        tableView.getColumns().addAll(nameColumn, genderColumn);

        // Add sample data
        ObservableList<Person> data = FXCollections.observableArrayList(
                new Person("John Doe", "Male"),
                new Person("Jane Doe", "Female"),
                new Person("Sam Smith", "Other")
        );
        tableView.setItems(data);

        // Set up the scene
        StackPane root = new StackPane();
        root.getChildren().add(tableView);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("TableView with ComboBox");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    // Data model class
    public static class Person {
        private final String name;
        private String gender;

        public Person(String name, String gender) {
            this.name = name;
            this.gender = gender;
        }

        public String getName() {
            return name;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }
    }
}